package fr.ups.jim.isi.tp6;

import java.awt.geom.Point2D;

import fr.lri.swingstates.canvas.CPolyLine;
import fr.lri.swingstates.canvas.CStateMachine;
import fr.lri.swingstates.canvas.Canvas;
import fr.lri.swingstates.sm.State;
import fr.lri.swingstates.sm.Transition;
import fr.lri.swingstates.sm.transitions.*;

public class PathTool extends CStateMachine {
	private CPolyLine polyLine;
	private Point2D p1;
	
	public State start, draw;
	
	public PathTool() {
		this(BUTTON1, NOMODIFIER);
	}
	
	public PathTool(final int button, final int modifier) {
		
		start = new State() {
			Transition press = new Press(button, modifier, ">> draw") {
				public void action() {
					Canvas canvas = (Canvas) getEvent().getSource();
					p1 = getPoint();
					polyLine = canvas.newPolyLine(p1);
					polyLine.setFilled(false);
				}
			};
		};
	
		draw = new State() {
			Transition draw = new Drag(button, modifier) {
				public void action() {
					polyLine.lineTo(getPoint());
				}
			};
			
			Transition stop = new Release(button, modifier, ">> start") {
				public void action() {
					polyLine.lineTo(getPoint());
					fireEvent(new ShapeCreatedEvent(PathTool.this, polyLine));
				}
			};
		};
	
	}
}
